
const WebSocketServer = require('ws').Server;

const wss = new WebSocketServer({ port: 9999 });

let app = null;
let window = null;

let clients = new Set();

wss.on('connection', function (ws) {
    console.log('connection:');
    clients.add(ws);

    ws.on('message', function (message) {
        console.log('message:', message);
        let data = JSON.parse(message);

        switch (data.cmd) {
            case 'openurl':
                if (window != null) {
                    window.loadURL(data.url);
                }
                break;

            case 'fullscreen':
                window.setFullScreen(data.flag);
                break;

            case 'close':
                window.close();
                break;

            case 'maximize':
                window.maximize();
                break;

            case 'minimize':
                window.minimize();
                break;

            case 'focus':
                app.focus();
                break;

            case 'quit':
                app.quit();
                break;
        }
    });

    ws.on('close', function (message) {
        console.log('close:');
        clients.delete(ws);
    });
});

module.exports = {
    init: function (w, a) {
        window = w;
        app = a;
    }
};
